package com.tiancikeji.zaoke.gui.http;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpLoader extends DefaultLoader{
	
	private String downloadDir;
	
	private String uploadDir;

	public String updateOrders(Map<Long, Long> orderIds, long zoneId) throws Throwable {
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(uploadURL + StringUtils.join(orderIds.keySet(), "%7C"));
		
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String response = httpClient.execute(httpget, responseHandler);
		return response;
	}

	@Override
	public String getOrders(long zoneId) throws Throwable {
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(downloadURL + zoneId);
		
		 HttpResponse response = httpClient.execute(httpget);  
         HttpEntity entity = response.getEntity();
         String content = ""; 
         if (entity != null) {  
             content = EntityUtils.toString(entity, "utf-8");  
             httpget.abort();  
             httpClient.getConnectionManager().shutdown();  
         } 
		
		FileWriter fw = new FileWriter(downloadDir + zoneId + "." + new SimpleDateFormat("yyyyMMdd").format(new Date()));
		fw.write(content);
		fw.flush();
		fw.close();
		
		return content;
	}

	@Override
	public Map<Long, String> getTokenOrders(long zoneId) {
		Map<Long, String> result = new HashMap<Long, String>();
		Scanner sc;
		try {
			File file = new File(uploadDir + zoneId + "." + new SimpleDateFormat("yyyyMMdd").format(new Date()));
			if(file.exists() && file.canRead()){
				sc = new Scanner(file);
				while(sc.hasNextLine()){
					String line = sc.nextLine();
					String[] lines = StringUtils.split(line, "\t");
					if(lines.length > 1)
						result.put(NumberUtils.toLong(lines[0]), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(NumberUtils.toLong(lines[1]))));
					else if(lines.length == 1)
						result.put(NumberUtils.toLong(lines[0]), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				}
				sc.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return result;
	}

	public String getDownloadDir() {
		return downloadDir;
	}

	public void setDownloadDir(String downloadDir) {
		this.downloadDir = downloadDir;
	}

	public String getUploadDir() {
		return uploadDir;
	}

	public void setUploadDir(String uploadDir) {
		this.uploadDir = uploadDir;
	}
}
